package com.ocean.proxy.api.base;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.Logger;

import com.ocean.core.common.system.MyLogManager;
import com.ocean.core.common.system.SystemContext;
import com.ocean.core.common.threadpool.Parameter;
import com.ocean.core.common.threadpool.workthread.AsynAbstractTask;
import com.ocean.core.common.threadpool.workthread.AsynInvokeResponse;
import com.ocean.core.common.threadpool.workthread.AsynTaskDisServer;
import com.ocean.persist.api.proxy.AdPullResponse;
import com.ocean.persist.api.proxy.gmobi.GmobiAdPullResponse;
import com.ocean.persist.common.ProxyConstants;
import com.ocean.proxy.serverdis.AdProxyAdhubTask;
import com.ocean.proxy.serverdis.TaskServerDispatcher;
public abstract class AbstractAdSupplier implements BaseAdSupplier{
     
	//public final Logger logger = LoggerFactory.getLogger("business");
	public static final String CHARSET_CODER="UTF-8";
	public  final Logger logger = MyLogManager.getLogger();
	protected static final Map<String, String> mobiles = new HashMap<String, String>(5);
	static{
		mobiles.put("CMCC", "46000");
		mobiles.put("CUCC", "46001");
		mobiles.put("CTCC", "46003");
	}

	protected String encode(String param){
		
		try {
			return URLEncoder.encode(param, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}
	public <T> T  convertV(T val,T defVal){
		try{
			if(val.getClass().isInstance(Integer.class)&&(Integer)val==0){
					return defVal;
				
			}else if(val.getClass().isInstance(String.class)&&StringUtils.isEmpty((String)val)){
				   return defVal;
			}
			
		}catch(Exception e){
			return defVal;
		}
		return val;
	}
	public boolean isAdapter(String dspName,float sourceH,float sourceW,float targetH,float targetW){
		int sizecheck=SystemContext.getDynamicPropertyHandler().getInt(ProxyConstants.IS_SPACE_CHECK,1);
		if(sizecheck==0){//校验总开关
			return true;
		}
		try{
			if(sourceH<=0||sourceW<=0){
				logger.error("no mapping ad to specified ad space size,joinDSP:{},sourceH:{},sourceW:{},targetH:{},targetW:{}",dspName,sourceH, sourceW, targetH, targetW);
				return false;
			}
			float  sizeRatio=sourceW/sourceH;
			float  arearRatio=sourceW*sourceH;
			if(Math.abs(sizeRatio-targetW/targetH)<=0.1&&Math.abs((arearRatio- targetW*targetH)/arearRatio)<=0.1){
				
				return true;
			}else{
				logger.error("no mapping ad to specified ad space size,joinDSP:{},sourceH:{},sourceW:{},targetH:{},targetW:{},size mapping:{},arear mapping",dspName,sourceH, sourceW, targetH, targetW,Math.abs(sizeRatio-targetW/targetH),Math.abs((arearRatio- targetW*targetH)/arearRatio));
				
				return false;
			}
		}catch(Exception e){
			logger.error("check space size mapping error,dspName:{},sourceH:{},sourceW:{},targetH:{},targetW:{}",dspName,sourceH, sourceW, targetH, targetW);
			
			return false;
		}

	}
   public String convOSVersion(String code){
	   if(StringUtils.isNotEmpty(code)&&code.indexOf(".")>0){
		   return code;
	   }
	   int _code=19;
	   try{
		   _code=Integer.parseInt(code);
	   }catch(Exception e){
		   
	   }
	   switch(_code){
	       case 1:
		         return "1.0";
	       case 2:
		         return "1.1";
	       case 3:
		         return "1.5";
	       case 4:
		         return "1.6";
	       case 5:
		         return "2.0";
	       case 6:
		         return "2.0.1";
	       case 7:
		         return "2.1";
	       case 8:
		         return "2.2.2";
	       case 9:
		         return "2.3";
	       case 10:
		         return "2.3.3";
	       case 11:
		         return "3.0";
	       case 12:
		         return "3.1";
	       case 13:
		         return "3.2.1";
	       case 14:
		         return "4.0.2";
	       case 15:
		         return "4.0.3";
	       case 16:
		         return "4.1.1"; 
	       case 17:
		         return "4.2.1";
	       case 18:
		         return "4.3.1";
	       case 19:
		         return "4.4.4";
	       case 20:
		         return "5.0";
	       case 21:
		         return "5.1";
	       case 22:
		         return "6.0";
	       case 23:
		         return "7.0";
	       case 24:
		         return "7.1.1";
		   default:
			     return "5.0";
	   }
   }
   public   String converMac(String mac){
	   if(StringUtils.isEmpty(mac)){
		   return null;
	   }
	   if(mac.indexOf(":")>0){
		   return mac;
	   }
	   StringBuffer buffer=new StringBuffer();
	   while(mac.length()>2){
		   if(buffer.length()>0){
			   buffer.append(":");
		   }
		   buffer.append(mac.substring(0, 2));
		   mac=mac.substring(2);
	   }
	   if(mac.length()>0){
		   buffer.append(":").append(mac);
	   }
	   return buffer.toString();
   }



}
